/*
 * JSMuffin - Another javascript canvas framework.
 * @author xshifty
 */

if (muffin && muffin.Canvas) muffin.Bezier = muffin.Bezier || muffin.Class({
	events:{
		onclick:function () {},
		onmousemove:function () {},
		onmousedown:function () {},
		onmouseup:function () {}
	},
	init:function (defaults_) {
		muffin.extend(this, {
			'backgroundColor':null,
			'borderColor':'#fff',
			'borderSize':1,
			'closePath':false,
			'nodes':[{'x':10,'y':10,'controlX':10,'controlY':10},{'x':150,'y':150,'controlX':150,'controlY':150}]
		}, defaults_);
	},
	renderize:function (ctx_, map_) {
		var ctx = ctx_ || false;
		var bounds = this.getBounds();
		var map = map_ ? {'x':bounds.x,'y':bounds.y} : false;

		if (!!ctx.toString().match(/CanvasRenderingContext2D/g) && this.nodes.length > 1) {
			var lastNode = muffin.clone(this.nodes[0]);
			var node = null;

			if (typeof this.backgroundColor == 'string') ctx.fillStyle = this.backgroundColor;
			if (typeof this.borderColor == 'string') ctx.strokeStyle = this.borderColor;
			if (typeof this.borderSize == 'number') ctx.lineWidth = this.borderSize;

			ctx.beginPath();
			for (var i = 1; i < this.nodes.length; i++) {
				node = this.nodes[i];
				
				lastNode = muffin.extend({
					'controlX':lastNode.x,
					'controlY':lastNode.y
				}, lastNode);
				
				if (map)
				lastNode = muffin.extend(lastNode, {
					'x':lastNode.x - map.x,
					'y':lastNode.y - map.y,
					'controlX':lastNode.controlX - map.x,
					'controlY':lastNode.controlY - map.y
				});
								
				node = muffin.extend({
					'controlX':node.x,
					'controlY':node.y
				}, node);

				if (map)
				node = muffin.extend(node, {
					'x':node.x - map.x,
					'y':node.y - map.y,
					'controlX':node.controlX - map.x,
					'controlY':node.controlY - map.y
				});
				
				ctx.moveTo(lastNode.x, lastNode.y);
				ctx.bezierCurveTo(lastNode.controlX, lastNode.controlY, node.controlX, node.controlY, node.x, node.y);
				
				lastNode = muffin.clone(node);
			}

			if (!!this.closePath) ctx.closePath();
			if (typeof this.backgroundColor == 'string') ctx.fill();
			if (typeof this.borderColor == 'string') ctx.stroke();
			
			delete(lastNode);
		}
	},
	getBounds:function () {
		var minCoord = {};
		var maxCoord = {};
		var bounds = {'x':0,'y':0,'width':0,'height':0};
		
		var node = this.nodes[0];

		minCoord.x = node.x;
		minCoord.y = node.y;
		maxCoord.x = node.x;
		maxCoord.y = node.y;
		
		for (var i in this.nodes) {
			node = this.nodes[i];
			
			minCoord.x = node.x < minCoord.x ? node.x : minCoord.x;
			minCoord.x = node.controlX < minCoord.x ? node.controlX : minCoord.x;
			minCoord.y = node.y < minCoord.y ? node.y : minCoord.y;
			minCoord.y = node.controlY < minCoord.y ? node.controlY : minCoord.y;
			
			maxCoord.x = node.x > maxCoord.x ? node.x : maxCoord.x;
			maxCoord.x = node.controlX > maxCoord.x ? node.controlX : maxCoord.x;
			maxCoord.y = node.y > minCoord.y ? node.y : maxCoord.y;
			maxCoord.y = node.controlY > maxCoord.y ? node.controlY : maxCoord.y;
		}
	
		bounds.x = minCoord.x;
		bounds.y = minCoord.y - Math.round(this.borderSize/2);
		bounds.width = maxCoord.x - minCoord.x;
		bounds.height = (maxCoord.y - minCoord.y) < 1 ? this.borderSize : (maxCoord.y - minCoord.y);
		
		delete(minCoord);
		delete(maxCoord);
		
		return bounds;
	}
});